/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.impl;

import dao.MateriaPrimaDao;
import java.util.List;
import model.Materiaprima;
import org.hibernate.Session;
import util.HibernateUtil;

/**
 *
 * @author Joel
 */
public class MateriaDaoImpl implements MateriaPrimaDao{

    @Override
    public void insertar(Materiaprima mateprima) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try
        {
            session.beginTransaction();
            session.save(mateprima);
            session.beginTransaction().commit();
        }catch(Exception e)
        {
            System.out.println("Error en insertar Materia Prima \n"+e.getMessage());
            session.beginTransaction().rollback();
        }
    }

    @Override
    public void actualizar(Materiaprima mateprima) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try
        {
            session.beginTransaction();
            session.merge(mateprima);
            session.beginTransaction().commit();
        }catch(Exception e)
        {
            System.out.println("Error en actualizar Materia Prima \n"+e.getMessage());
            session.beginTransaction().rollback();
        }
    }

    @Override
    public void eliminar(Materiaprima mateprima) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try
        {
            session.beginTransaction();
            session.delete(mateprima);
            session.beginTransaction().commit();
        }catch(Exception e)
        {
            System.out.println("Error en eliminar Materia Prima \n"+e.getMessage());
            session.beginTransaction().rollback();
        }
    }

    @Override
    public Materiaprima buscarporId(Integer id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        return (Materiaprima) session.load(Materiaprima.class, id);
    }

    @Override
    public List<Materiaprima> buscarTodos() {
        Session session = HibernateUtil.getSessionFactory().openSession();
       return session.createQuery("from Materiaprima").list();
    }
    
}
